package com.flipdf.viewer.excel;

import java.awt.Rectangle;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.flipdf.viewer.excel.Excel.Row;

public class ExcelDialog extends JDialog {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JLabel jLabel = null;
	private JComboBox jComboBoxColumn = null;
	private JButton jButtonAdd = null;
	private JButton jButtonCancel = null;
	
	private List<Row> rows;  //  @jve:decl-index=0:
	private boolean isOk;
	private String column;

	public ExcelDialog(JFrame owner) {
		super(owner);
		this.setModal(true);
		initialize();
		
		//hard code for test
		open("c:/Book1.xls");
	}
	
	private void open(String path) {
		try {
			Excel excel = new Excel(path);
			rows = excel.getRows(0);
			Row row = rows.get(0);
			List<String> header = row.getRowData();
			for (String s : header) {
				jComboBoxColumn.addItem(s);				
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public boolean isOk() {
		return isOk;
	}
	
	public String getColumn() {
		return column;
	}
	
	public List<Row> getRows() {
		return rows;
	}

	private void initialize() {
		this.setSize(370, 241);
		this.setContentPane(getJContentPane());
	}

	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(54, 49, 69, 18));
			jLabel.setText("Column:");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(jLabel, null);
			jContentPane.add(getJComboBoxColumn(), null);
			jContentPane.add(getJButtonAdd(), null);
			jContentPane.add(getJButtonCancel(), null);
		}
		return jContentPane;
	}

	private JComboBox getJComboBoxColumn() {
		if (jComboBoxColumn == null) {
			jComboBoxColumn = new JComboBox();
			jComboBoxColumn.setBounds(new Rectangle(137, 44, 209, 27));
		}
		return jComboBoxColumn;
	}

	private JButton getJButtonAdd() {
		if (jButtonAdd == null) {
			jButtonAdd = new JButton();
			jButtonAdd.setBounds(new Rectangle(79, 115, 61, 28));
			jButtonAdd.setText("Add");
			jButtonAdd.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					column = jComboBoxColumn.getSelectedItem().toString();
					isOk = true;
					setVisible(false);
				}
			});
		}
		return jButtonAdd;
	}

	private JButton getJButtonCancel() {
		if (jButtonCancel == null) {
			jButtonCancel = new JButton();
			jButtonCancel.setBounds(new Rectangle(187, 115, 81, 28));
			jButtonCancel.setText("Cancel");
			jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					isOk = false;
					setVisible(false);
				}
			});
		}
		return jButtonCancel;
	}

}